<?php
/**
 * Created by PhpStorm.
 * User: 程序猴
 * Date: 2018/4/9
 *  Q Q: 505413
 * Time: 10:36
 */

namespace app\admin\controller;
use app\admin\controller\Base;
use app\admin\model\AuthRule as AuthRuleModel;
use think\Data;
use think\Db;
class Rule extends Base
{
    //权限列表
    public function index()
    {
        //树形分类
        $list = Db::name('auth_rule')->order('level asc , sort asc')->select();
        $list = Data::tree($list,'title','id','pid');
        $this->assign('list',$list);
        return $this->fetch();
    }
    //添加权限
    public function ruleAdd()
    {
        if(request()->isAjax()){
            $param = input('post.');
            $plevel = Db::name('auth_rule')->where(array('id'=>$param['pid']))->field('level')->find();
            if(!empty($plevel)){
                $param['level'] = $plevel['level'] + 1;
            }else{
                $param['level'] = 0;
            }
            $param['create_time'] = time();
            $param['update_time'] = time();
            $res = Db::name('auth_rule')->insert($param);
            if($res){
                return json(['code' => 1, 'url' => '/rule/index', 'msg' => '添加权限成功']);
            }else{
                return json(['code' => -1, 'url' => '/rule/index', 'msg' => '添加权限失败']);
            }
        }
        $authruleres = Db::name('auth_rule')->select();
        $authruleres = Data::tree($authruleres,'title','id','pid');
        $this->assign('authruleres',$authruleres);

        return $this->fetch();
    }
    //编辑权限
    public function ruleEdit(){
        $id = input('id');
        $authrule = Db::name('auth_rule')->where(array('id'=>$id))->find();
        $authruleres = Db::name('auth_rule')->select();
        $authruleres = Data::tree($authruleres,'title','id','pid');
        if(request()->isAjax()){
            $param = input('post.');
            $plevel = Db::name('auth_rule')->where(array('id'=>$param['pid']))->field('level')->find();
            if(!empty($plevel)){
                $param['level'] = $plevel['level'] + 1;
            }else{
                $param['level'] = 0;
            }
            $param['update_time'] = time();
            $data = Db::name("auth_rule")->where(array('id'=>$id))->update($param);
            if(false === $data){
                return json(['code' => -1, 'url' => 'group/index', 'msg' => '修改权限失败']);
            }else{
                return json(['code' => 1, 'url' => 'group/index', 'msg' => '修改权限成功']);
            }
        }
        $this->assign('authrule',$authrule);
        $this->assign('authruleres',$authruleres);

        return $this->fetch();
    }
    //删除权限
    public function ruleDel(){
        $id = input('id');
        $authRule=new AuthRuleModel();
        $authRule->getparentid($id);
        $authRuleIds=$authRule->getchilrenid($id);
        if(empty($authRuleIds)){
            $authRuleIds[]=$id;
            $del= AuthRuleModel::destroy($authRuleIds);
            if($del){
                return json(['code' => 1, 'url' => '/rule/index', 'msg' => '删除权限成功']);
            }else{
                return json(['code' => -1, 'url' => '/rule/index', 'msg' => '删除权限失败！']);
            }
        }else{
            return json(['code' => -2, 'url' => '/rule/index', 'msg' => '该类目下存在子类目，请先删除子类目']);
        }

    }
    //菜单排序
    public function sort()
    {
        if (request()->isAjax()){
            $model  =   Db::name('auth_rule');
            $data   =   input('post.');
            if(empty($data['id']) || empty($data['sort']) || count($data['id'])!==count($data['sort'])){
                return json(['code' => 0, 'msg' => '参数错误']);
            }
            for ($i=0;$i<count($data['id']);$i++){
                $res = $model->where(array('id' => $data['id'][$i]))->setField(array('sort' => $data['sort'][$i]));
                if($res === false)
                    return json(['code' => 0, 'msg' => '排序更新失败']);
            }
            return json(['code' => 1, 'msg' => '排序更新成功']);
        }
    }
}